<!DOCTYPE html>
<html lang="en">

<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    
        
                <meta name="keywords" content="爱前端 | 爱工作 | 爱生活">
                
                    
                            <meta name="description" content="This is My blog">
                            
                                <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
                                <title>
                                    
                                        vue keepAlive移除缓存(代替this.$destroy()) |
                                            
                                                一只正在成长的程序猿
                                </title>
                                
                                    <link rel="shortcut icon" href="/images/headPortrait.png">
                                    
                                        <link rel="stylesheet" href="/css/style.css">
                                            
                                                <link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">
                                                    
                                                        <script src="/js/pace.min.js"></script>
</head></html>

    <body>
        <main class="content">
            <section class="outer">
    <article id="post-解决keepAlive缓存bug" class="article article-type-post" itemscope itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      vue keepAlive移除缓存(代替this.$destroy())
    </h1>
  
  




      </header>
    

    
      <div class="article-meta">
        <a href="/2019/08/07/解决keepAlive缓存bug/" class="article-date">
  <time datetime="2019-08-07T02:31:32.536Z" itemprop="datePublished">2019-08-07</time>
</a>
        
  <div class="article-category">
    <a class="article-category-link" href="/categories/技术/">技术</a>
  </div>

      </div>
    

    <div class="article-entry" itemprop="articleBody">
      

      

      
        <h3 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h3><blockquote>
<p>注册页面有一个，点击去查看协议的入口，此时当客户填写了数据，从协议页面回来之后数据就会清空，所以我选择了用keepAlive，但是用第一种方式发现缓存的始终都是第一次填写的东西，如果用this.$destroy()就再也不能缓存了，所以经过多方探查，用了第二种方案完美解决。…</p>
</blockquote>
<a id="more"></a>

<h3 id="需求-1"><a href="#需求-1" class="headerlink" title="需求"></a>需求</h3><blockquote>
<p>注册页面有一个，点击去查看协议的入口，此时当客户填写了数据，从协议页面回来之后数据就会清空，所以我选择了用keepAlive，但是用第一种方式发现缓存的始终都是第一次填写的东西，如果用this.$destroy()就再也不能缓存了，所以经过多方探查，用了第二种方案完美解决。</p>
</blockquote>
<h4 id="router-js"><a href="#router-js" class="headerlink" title="router.js"></a>router.js</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> Router <span class="keyword">from</span> <span class="string">'vue-router'</span></span><br><span class="line"></span><br><span class="line">Vue.use(Router) </span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Router(&#123;</span><br><span class="line">    routes: [&#123;</span><br><span class="line">            path: <span class="string">'/register'</span>,</span><br><span class="line">            name: <span class="string">'register'</span>,</span><br><span class="line">            component: <span class="function"><span class="params">resolve</span> =&gt;</span> <span class="built_in">require</span>([<span class="string">'@/pages/sign/register.vue'</span>], resolve),</span><br><span class="line">            meta: &#123;</span><br><span class="line">                keepAlive: <span class="literal">true</span>, <span class="comment">// 此组件需要被缓存</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;]</span><br></pre></td></tr></table></figure>

<h4 id="1-第一种方案"><a href="#1-第一种方案" class="headerlink" title="1.第一种方案"></a>1.第一种方案</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">beforeRouteLeave(to, <span class="keyword">from</span>, next) &#123;</span><br><span class="line">   <span class="keyword">if</span> (to.name === <span class="string">"useAgreement"</span>) &#123;</span><br><span class="line">     <span class="keyword">from</span>.meta.keepAlive = <span class="literal">true</span>; <span class="comment">//当我们进入到C时开启B的缓存</span></span><br><span class="line">     next();</span><br><span class="line">   &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">     <span class="keyword">from</span>.meta.keepAlive = <span class="literal">false</span>;</span><br><span class="line">     <span class="keyword">this</span>.$destroy()</span><br><span class="line">     next(); <span class="comment">//当我们前进的不是C时我们让B页面刷新</span></span><br><span class="line">   &#125;</span><br><span class="line"> &#125;,</span><br></pre></td></tr></table></figure>

<h4 id="2-第二种方案：在路由跳转里动态判断移除缓存"><a href="#2-第二种方案：在路由跳转里动态判断移除缓存" class="headerlink" title="2.第二种方案：在路由跳转里动态判断移除缓存"></a>2.第二种方案：在路由跳转里动态判断移除缓存</h4><p>使用Vue.mixin的方法拦截了路由离开事件，并在该拦截方法中实现<code>后退时销毁页面缓存</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用Vue.mixin的方法拦截了路由离开事件，并在该拦截方法中实现了销毁页面缓存的功能。</span></span><br><span class="line"> Vue.mixin(&#123;</span><br><span class="line">     beforeRouteLeave: <span class="function"><span class="keyword">function</span>(<span class="params">to, from, next</span>) </span>&#123;</span><br><span class="line">         <span class="comment">// 默认是缓存的 在来清除</span></span><br><span class="line">         <span class="comment">// 1.用tag标记控制 判断上下级</span></span><br><span class="line">         <span class="comment">// if (from &amp;&amp; from.meta.tag &amp;&amp; to.meta.tag &amp;&amp; (from.meta.tag-to.meta.tag&lt;1))</span></span><br><span class="line">         <span class="comment">// 2.直接用组件名字来写 不够通用</span></span><br><span class="line">         <span class="comment">// if (from.path == '/docMng' &amp;&amp; to.path == '/docMng/docDetail') &#123;</span></span><br><span class="line">         <span class="comment">// 3. 用包含关系来判断 通用</span></span><br><span class="line">         <span class="built_in">console</span>.log(to, <span class="keyword">from</span>)</span><br><span class="line">         <span class="keyword">if</span> (to.path.indexOf(<span class="string">'useAgreement'</span>) != <span class="number">-1</span> &amp;&amp; <span class="keyword">from</span>.name == <span class="string">'register'</span>) &#123;</span><br><span class="line">             <span class="keyword">from</span>.meta.keepAlive = <span class="literal">true</span>;</span><br><span class="line">         &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                 <span class="comment">// if (from &amp;&amp; from.meta.tag &amp;&amp; to.meta.tag &amp;&amp; (from.meta.tag-to.meta.tag&lt;1))&#123;</span></span><br><span class="line">             <span class="keyword">if</span> (<span class="keyword">this</span>.$vnode &amp;&amp; <span class="keyword">this</span>.$vnode.data.keepAlive &amp;&amp; <span class="keyword">from</span>.name == <span class="string">'register'</span> &amp;&amp; to.path.indexOf(<span class="string">'home'</span>) != <span class="number">-1</span>) &#123;</span><br><span class="line">                 <span class="keyword">if</span> (<span class="keyword">this</span>.$vnode.parent &amp;&amp; <span class="keyword">this</span>.$vnode.parent.componentInstance &amp;&amp; <span class="keyword">this</span>.$vnode.parent.componentInstance.cache) &#123;</span><br><span class="line">                     <span class="keyword">if</span> (<span class="keyword">this</span>.$vnode.componentOptions) &#123;</span><br><span class="line">                         <span class="keyword">var</span> key = <span class="keyword">this</span>.$vnode.key == <span class="literal">null</span> ? <span class="keyword">this</span>.$vnode.componentOptions.Ctor.cid + (<span class="keyword">this</span>.$vnode.componentOptions.tag ? <span class="string">`::<span class="subst">$&#123;<span class="keyword">this</span>.$vnode.componentOptions.tag&#125;</span>`</span> : <span class="string">''</span>) : <span class="keyword">this</span>.$vnode.key</span><br><span class="line">                         <span class="keyword">var</span> cache = <span class="keyword">this</span>.$vnode.parent.componentInstance.cache</span><br><span class="line">                         <span class="keyword">var</span> keys = <span class="keyword">this</span>.$vnode.parent.componentInstance.keys</span><br><span class="line">                         <span class="keyword">if</span> (cache[key]) &#123;</span><br><span class="line">                             <span class="keyword">if</span> (keys.length) &#123;</span><br><span class="line">                                 <span class="keyword">var</span> index = keys.indexOf(key)</span><br><span class="line">                                 <span class="keyword">if</span> (index &gt; <span class="number">-1</span>) &#123;</span><br><span class="line">                                     keys.splice(index, <span class="number">1</span>)</span><br><span class="line">                                 &#125;</span><br><span class="line">                             &#125;</span><br><span class="line">                             <span class="keyword">delete</span> cache[key]</span><br><span class="line">                         &#125;</span><br><span class="line">                     &#125;</span><br><span class="line">                 &#125;</span><br><span class="line">                 <span class="keyword">from</span>.meta.keepAlive = <span class="literal">true</span>;</span><br><span class="line">             &#125;</span><br><span class="line">             <span class="comment">// this.$destroy()</span></span><br><span class="line">         &#125;</span><br><span class="line">         next()</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;)</span><br></pre></td></tr></table></figure>

<p><a href="https://wanyaxing.com/blog/20180724141008.html" target="_blank" rel="noopener">参考链接1</a></p>
<p><a href="https://blog.csdn.net/weixin_40425232/article/details/88852125" target="_blank" rel="noopener">参考链接2</a>…</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="wgjh5.github.io/2019/08/07/解决keepAlive缓存bug/" data-id="cjzdy95vh0028acu8xikpro6j"
         class="article-share-link">分享</a>
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/keepalive/">keepalive</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/vue/">vue</a></li></ul>

    </footer>

  </div>

  
    
  <nav class="article-nav">
    
      <a href="/2019/08/07/Echarts/" class="article-nav-link">
        <strong class="article-nav-caption">前一篇</strong>
        <div class="article-nav-title">
          
            Vue中Echart用法
          
        </div>
      </a>
    
    
  </nav>


  

  
    
    <div class="gitalk" id="gitalk-container"></div>
    <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
        <script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
            <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
                <script type="text/javascript">
                    var gitalk = new Gitalk({
                        clientID: '64ce5333a42f02cf200d',
                        clientSecret: '7d37a3d8d3c9e215de06d3078bd1894053368739',
                        repo: 'wgjh5.github.io',
                        owner: 'wgjh5',
                        admin: ['wgjh5'],
                        // id: location.pathname,      // Ensure uniqueness and length less than 50
                        id: md5(location.pathname),
                        distractionFreeMode: true, // Facebook-like distraction free mode
                        pagerDirection: 'last'
                    })

                    gitalk.render('gitalk-container')
                </script>
                
  

</article>



</section>
                <footer class="footer">
  <div class="outer">
    <div class="float-right">
      <ul class="list-inline">
  
    <li><i class="fe fe-smile-alt"></i> <span id="busuanzi_value_site_uv"></span></li>
  
</ul>
    </div>
    <ul class="list-inline">
      <li>&copy; 2019 一只正在成长的程序猿</li>
      <li>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></li>
      <li>Theme  <a href="https://github.com/zhwangart/hexo-theme-ocean">Ocean</a></li>
    </ul>
  </div>
</footer>

        </main>
        <aside class="sidebar">
            <button class="navbar-toggle"></button>

<nav class="navbar">
  
    <div class="logo">
      <a href="/"><img src="/images/shark.svg" alt="一只正在成长的程序猿"></a>
    </div>
  
  <ul class="nav nav-main">
    
      <li class="nav-item">
        <a class="nav-item-link" href="/">主页</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/archives">归档</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/about">关于</a>
      </li>
    
    <li class="nav-item">
      <a class="nav-item-link nav-item-search" title="搜索">
        <i class="fe fe-search"></i>
        搜索
      </a>
    </li>
  </ul>
</nav>

<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      <div class="totop" id="totop">
  <i class="fe fe-rocket"></i>
</div>
    </li>
    <li class="nav-item">
      
        <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
          <i class="fe fe-feed"></i>
        </a>
      
    </li>
  </ul>
</nav>

<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
        </aside>
        <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/lazyload.min.js"></script>
<script src="/js/snap.svg-min.js"></script>
<script src="/js/busuanzi-2.3.pure.min.js"></script>


  <script src="/fancybox/jquery.fancybox.min.js"></script>



  <script src="/js/search.js"></script>


<script src="/js/ocean.js"></script>

    </body>

    </html>